﻿Namespace AnalysisTechniques
    Public Class MovingAverage
        Inherits AnalysisTechnique

        <Input("The color for the average line.")>
        Public Property Color As Color = Colors.Red
        <Input("The number of bars to use to determine the average price.")>
        Public Property AveragingAmount As Integer = 10
        <Input("The number of bars to offset the average line to the left.")>
        Public Property OffsetAmount As Integer = 10

        Public Sub New(ByVal chart As Chart)
            MyBase.New(chart)
            Description = "Draws an average curve that follows the price."
        End Sub

        Dim plot As Plot
        Dim last10bars As Decimal()
        Protected Overrides Sub Begin()
            MyBase.Begin()
            plot = New Plot
            plot.AnalysisTechnique = Me
            plot.UseNegativeCoordinates = False
            'plot.IsSelectable = False
            plot.Pen = New Pen(New SolidColorBrush(Color), 1)
            AddObjectToChart(plot)
            ReDim last10bars(AveragingAmount - 1)
        End Sub

        Protected Overrides Sub Main()

        End Sub

        Protected Overrides Sub NewBar()
            If CurrentBar.Number >= AveragingAmount Then
                For i = CurrentBar.Number - AveragingAmount To CurrentBar.Number - 1
                    last10bars(i - (CurrentBar.Number - AveragingAmount)) = Chart.bars(i).Data.Close
                Next
                plot.Points.Add(New Point(CurrentBar.Number - OffsetAmount, last10bars.Average))
                If IsLastBarOnChart Then plot.RefreshVisual()
            End If
        End Sub
        Public Overrides Property Name As String = "MovingAverage"
    End Class

End Namespace

